// 验证同步结果的脚本
print('Verifying sync results...');

// 连接源数据库
const sourceDB = connect('mongodb-source:27017/testdb');
sourceDB.auth('admin', 'passwordA');

// 连接目标数据库
const targetDB = connect('mongodb-target:27017/testdb');
targetDB.auth('admin', 'passwordB');

// 验证集合数量
print('\n=== Collection Count Verification ===');
const collections = ['users', 'products', 'orders'];

collections.forEach(collection => {
  const sourceCount = sourceDB[collection].countDocuments();
  const targetCount = targetDB[collection].countDocuments();
  
  print(`${collection}: Source=${sourceCount}, Target=${targetCount}, Match=${sourceCount === targetCount ? '✓' : '✗'}`);
});

// 验证数据内容
print('\n=== Data Content Verification ===');
collections.forEach(collection => {
  print(`\nVerifying ${collection} collection:`);
  
  const sourceDocs = sourceDB[collection].find().toArray();
  const targetDocs = targetDB[collection].find().toArray();
  
  if (sourceDocs.length !== targetDocs.length) {
    print(`  ✗ Document count mismatch: Source=${sourceDocs.length}, Target=${targetDocs.length}`);
    return;
  }
  
  // 比较每个文档
  let matchCount = 0;
  sourceDocs.forEach((sourceDoc, index) => {
    const targetDoc = targetDocs[index];
    if (JSON.stringify(sourceDoc) === JSON.stringify(targetDoc)) {
      matchCount++;
    } else {
      print(`  ✗ Document ${index} mismatch:`);
      print(`    Source: ${JSON.stringify(sourceDoc)}`);
      print(`    Target: ${JSON.stringify(targetDoc)}`);
    }
  });
  
  print(`  ✓ ${matchCount}/${sourceDocs.length} documents match`);
});

// 验证数据库统计信息
print('\n=== Database Statistics ===');
const sourceStats = sourceDB.stats();
const targetStats = targetDB.stats();

print(`Source DB - Collections: ${sourceStats.collections}, Objects: ${sourceStats.objects}`);
print(`Target DB - Collections: ${targetStats.collections}, Objects: ${targetStats.objects}`);

print('\nSync verification completed!'); 